AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include( "shared.lua" )
AddCSLuaFile( "player_class/rebelplayers.lua" )

resource.AddFile("models/player/Group03m/Female_07.mdl")
resource.AddFile("models/player/Group03m/Male_04.mdl")
resource.AddFile("models/player/Group03m/male_06.mdl")
resource.AddFile("models/player/Group03m/male_08.mdl")
resource.AddFile("models/weapons/v_medkit.mdl")
resource.AddFile("models/items/w_medkit.mdl")
resource.AddFile("models/weapons/v_stunstick.mdl")
resource.AddFile("materials/models/weapons/v_stunstick/v_stunstick_diffuse.vtf")
resource.AddFile("materials/models/weapons/v_stunstick/v_stunstick_diffuse.vmt")
resource.AddFile("materials/models/weapons/v_stunstick/v_stunstick_normal.vtf")

function GM:OnPreRoundStart( num )
	
	game.CleanUpMap()
	
	UTIL_StripAllPlayers()
	StripWorld()
	UTIL_SpawnAllPlayers()
	UTIL_FreezeAllPlayers()

end

function GM:OnRoundStart( num )
	
	for t=1,2 do
		for k,v in pairs( team.GetPlayers( t ) ) do
			v:SetFrags( 0 )
		end
	end
		
	UTIL_UnFreezeAllPlayers()

end

function GM:OnRoundResult( t )
	
	team.AddScore( t, 1 )
	
	if team.GetScore( t ) >= 3 then
		timer.Simple( 5, function() GAMEMODE:EndOfGame( true ) end )
	end
	
end

function GM:RoundTimerEnd()

	if team.TotalFrags( TEAM_REBEL ) < team.TotalFrags( TEAM_COMBINE ) then
		GAMEMODE:RoundEndWithResult( TEAM_COMBINE )
	elseif team.TotalFrags( TEAM_REBEL ) > team.TotalFrags( TEAM_COMBINE ) then
		GAMEMODE:RoundEndWithResult( TEAM_REBEL )
	else
		GAMEMODE:RoundEndWithResult( ROUND_RESULT_DRAW )
	end
	

end

function GM:CheckRoundEnd()

end

function GM:OnRoundWinner()

end

function StripWorld()
    // Remove all weapons from the world.
	//local wpns = ents.FindByClass("weapon_*");
	//local itms = ents.FindByClass("item_*");
	
	for k,v in pairs( ents.FindByClass("weapon_*") ) do
        v:Remove()
    end
	// Remove all items from the world.
	for k,v in pairs( ents.FindByClass("item_*") ) do
        v:Remove()
    end
end

function GM:DoPlayerDeath( ply, attacker, dmginfo )
	
	ply:CallClassFunction( "OnDeath", attacker, dmginfo )
	ply:CreateRagdoll()
	local rag = ply:GetRagdollEntity( )
	rag:SetCollisionGroup( COLLISION_GROUP_DEBRIS )
	ply:AddDeaths( 1 )
	
	if ( attacker:IsValid() && attacker:IsPlayer() ) then
		if ( attacker == ply ) then
			if ( GAMEMODE.TakeFragOnSuicide ) then
				attacker:AddFrags( -1 )
				if ( GAMEMODE.TeamBased && GAMEMODE.AddFragsToTeamScore ) then
					team.AddScore( attacker:Team(), -1 )
				end
			end
		else
			attacker:AddFrags( 1 )
			if ( GAMEMODE.TeamBased && GAMEMODE.AddFragsToTeamScore ) then
				team.AddScore( attacker:Team(), 1 )
			end
		end
	end
	
	if ( GAMEMODE.EnableFreezeCam && IsValid( attacker ) && attacker != ply ) then
		ply:SpectateEntity( attacker )
		ply:Spectate( OBS_MODE_FREEZECAM )
	end
end